﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebEntities;
using com.subb.ProductInfo;
using com.subb.Tools.Pager;

namespace WebUI.Controllers
{
    public class ProductsController : Controller
    {
        private const int RowCount = 32;

        public ProductsController()
            : base()
        {
            ViewBag.ActMenu = "products";
        }

        public ActionResult Index(int? id, int page = 1)
        {
            page = page < 1 ? 1 : page;
            int start = (page - 1) * RowCount;
            PP_ProductCataDAL pcdal = new PP_ProductCataDAL();
            PP_ProductCata pcata;
            if (!id.HasValue)
            {
                pcata = pcdal.GetFirstProductCata();
            }
            else
            {
                pcata = pcdal.GetProductCata(id.Value);
                if (pcata == null)
                {
                    pcata = pcdal.GetFirstProductCata();
                }
            }
            ViewBag.ThisCataID = pcata.ProdCataID;
            ViewBag.ThisCataName = pcata.ProdCataName;
            PP_ProductDAL pdal = new PP_ProductDAL();
            int ttlCount;
            List<PP_Product> prodList = pdal.GetAllProduct(pcata.ProdCataID, start, RowCount, out ttlCount);
            PagedList<PP_Product> pglist = prodList.ToPagedList(page, RowCount, ttlCount);
            return View(pglist);
        }

        public ActionResult TopSubMenu()
        {
            PP_ProductCataDAL pcdal = new PP_ProductCataDAL();
            List<PP_ProductCata> cataList = pcdal.GetAllProductCata();
            return PartialView(cataList);
        }

        public ActionResult Details(int id)
        {
            PP_ProductDAL pdal = new PP_ProductDAL();
            PP_Product product = pdal.GetProduct(id);
            List<PP_ProductImg> pimglist = new List<PP_ProductImg>();
            if (product != null)
            {
                PP_ProductImgDAL pidal = new PP_ProductImgDAL();
                pimglist = pidal.GetAllProductImg(id);
            }
            ViewBag.ProdImgList = pimglist;
            return View(product);
        }

        public ActionResult LeftMenu(int cataId)
        {
            ViewBag.CataID = cataId;
            PP_ProductCataDAL pcdal = new PP_ProductCataDAL();
            List<PP_ProductCata> cataList = pcdal.GetAllProductCata();
            return PartialView(cataList);
        }

        public ActionResult SearchProduct(string seakey, int id = 1)
        {
            int page = id < 1 ? 1 : id;
            int start = (page - 1) * RowCount;
            ViewBag.SearchKey = seakey;
            PP_ProductDAL pdal = new PP_ProductDAL();
            int ttlCount;
            List<PP_Product> prodList = pdal.SearchByName(seakey, start, RowCount, out ttlCount);
            PagedList<PP_Product> pglist = prodList.ToPagedList(page, RowCount, ttlCount);
            ViewBag.SearchCount = ttlCount;
            return View(pglist);
        }

        [HttpPost]
        public ActionResult SearchProduct(string seakey)
        {
            return RedirectToAction("SearchProduct", new { seakey = seakey });
        }
    }
}
